<?php

declare(strict_types=1);

namespace app\common\dao\im;

use app\common\model\im\ImGroupUser;
use app\common\traits\DaoTrait;

/**
 * 聊天群用户关联
 * @author:kaige
 * @update:2022-06-12
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class GroupUserDao
{
  // 当前操作的模型
  protected $model = null;

  // 对应模型的主键，默认为id
  protected $pk = 'id';

  // 对应模型默认查询的字段
  protected $name = 'uid';

  // 对应模型允许查询的字段(默认全部查询)
  protected $allowField = ['*'];

  // 允许单字段更新
  protected $uaField = ['is_show_nick', 'is_top'];

  // 查询结果-要隐藏的字段 默认空数组
  protected $hideField = [];

  public function __construct()
  {
    $this->model = new ImGroupUser();
  }
  use DaoTrait;

  /**
   * 获取用户加入的所有群
   */
  public function getListByUserId($id = 0,$field = ['*'])
  {
    $res = $this->model->with('glgroup')->field($field)->where('uid', $id)->select()->toArray();
    if ($res) {
      foreach ($res as $k => $v) {
        if (isset($v['avatar'])) {
          $res[$k]['avatar'] = _img($v['avatar']);
        }
        if (isset($v['chat_bg'])) {
          $res[$k]['chat_bg'] = _img($v['chat_bg']);
        }
        $res[$k]['chat_type'] = 2; //群聊为2
      }
    }
    return $res;
  }

  /**
   * 根据群ID获取群所有成员
   */
  public function getImGroupUserList($id = 0)
  {
    return $this->model->with('glgroup')->field('*')->where('group_id', $id)->select()->toArray();
  }

  /**
   * 根据群ID获取群所有成员
   */
  public function getImGroupUserListSzim($id = 0)
  {
    return $this->model->with(['glgroup','gluser'])->field('*')->where('group_id', $id)->select()->toArray();
  }

  /**
   * 根据群ID获取群所有成员ID
   */
  public function getImGroupUserIds($id = 0)
  {
    return $this->model->where('group_id', $id)->column('uid');
  }
}
